package com.community.web.master.controller;


import com.community.common.result.Result;
import com.community.model.entity.Goods;
import com.community.web.master.domain.dto.GoodsDto;
import com.community.web.master.domain.dto.PageGoodsDto;
import com.community.web.master.domain.vo.GoodsWithCategoryNameVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.mapper.GoodsMapper;
import com.community.web.master.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/master/goods")
@Api(tags = "商品接口")
@Slf4j
public class GoodsController {
    @Autowired
    IGoodsService iGoodsService;
    @GetMapping("/pageQuery")
    @ApiOperation("商品分页查询")
    public Result<PageResult> pageQuery(PageGoodsDto pageGoodsDto){
        log.info("商品分页查询"+pageGoodsDto);
        PageResult result =iGoodsService.pageQuery(pageGoodsDto);
        return Result.success(result);
    }
    @DeleteMapping
    @ApiOperation("批量删除商品")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        log.info("批量删除商品"+ ids);
        iGoodsService.removeBatchByIds(ids);
        return Result.success();
    }

    @PostMapping
    @ApiOperation("新增商品")
    public Result add(@RequestBody GoodsDto goodsDto){
        log.info("新增商品"+goodsDto);
        iGoodsService.add(goodsDto);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id获取商品")
    public Result<GoodsWithCategoryNameVo> getById(@PathVariable Integer id){
        log.info("根据id获取商品"+id);
        GoodsWithCategoryNameVo result = iGoodsService.getGoodsById(id);
        return Result.success(result);
    }

    @PutMapping
    @ApiOperation("更新商品")
    public Result updateGoods(@RequestBody GoodsDto goodsDto){
        log.info("更新商品"+goodsDto);
        iGoodsService.updateGoods(goodsDto);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("获取所有商品")
    public Result<List<GoodsWithCategoryNameVo>> list(){
        return Result.success(iGoodsService.getAllGoodsWithCategoryName());
    }


}
